use crate::handle::PacketHandler;
use crate::packet::Packet;
use crate::Action;
use crate::util::print_packet_chars;
use crate::packet::PacketType;
use crate::util::print_packet_bytes;

pub struct MysqlHandler {}

impl PacketHandler for MysqlHandler {

    fn handle_request(&mut self, p: &Packet) -> Action {
        print_packet_chars(&p.bytes);
        match p.packet_type() {
            Ok(PacketType::ComQuery) => {
                // ComQuery packets just contain a SQL string as the payload
                let slice = &p.bytes[5..];

                // convert the slice to a String object
                let sql = String::from_utf8(slice.to_vec()).expect("Invalid UTF-8");

                // log the query
                println!("SQL: {}", sql);

                // dumb example of conditional proxy behavior
                if sql.contains("avocado") {
                    // take over processing of this packet and return an error packet
                    // to the client
                    Action::Error {
                        code: 1064, // error code
                        state: [0x31, 0x32, 0x33, 0x34, 0x35], // sql state
                        msg: String::from("Proxy rejecting any avocado-related queries")
                    }
                } else {
                    // pass the packet to MySQL unmodified
                    Action::Forward
                }
            },
            _ => Action::Forward
        }
    }

    fn handle_response(&mut self, p: &Packet) -> Action {
        // forward all responses to the client
        print_packet_bytes(&p.bytes);
        Action::Forward
    }

}